#include "组员.h"

int overmess(int fd, W_REGIST lg)
{
    MSG m1;
    PRINTFTASK username;
    strcpy(username.name, lg.name);
    memcpy(m1.data, &username, sizeof(username));
    m1.type = userprinttask;
    send(fd, &m1, sizeof(m1), 0);
    while (1)
    {
        if (recv(fd, &m1, sizeof(m1), 0) <= 0)
        {
            perror("接收任务信息失败");
            return -1;
        }
        if (m1.type == taskinfo)
        {
            TASK *task = (TASK *)m1.data;
            printf("任务ID: %d, 任务名称: %s, 完成进度: %s\n", task->id, task->name, task->progress);
        }
        else if (m1.type == over)
        {
            printf("任务信息已全部接收完毕\n");
            break; // 收到结束标志，退出循环
        }
        else
        {
            printf("未知的消息类型\n");
        }
    } 
    printf("\n");

    OVPS overme;
    strcpy(overme.name, lg.name);
    printf("请选择你要完成的任务的ID: ");
    scanf("%d", &overme.id);
    getchar();
    printf("请输入完成进度: (如果已经完成请输入‘完成’)");
    scanf("%s", overme.overpa);
    getchar();
    m1.type = overmes;
    memcpy(m1.data, &overme, sizeof(overme));
    send(fd, &m1, sizeof(m1), 0);
    if (recv(fd, &m1, sizeof(m1), 0) <= 0)
    {
        perror("接收任务信息失败");
        return -1;
    }
    if (m1.type == yes)
    {
        printf("修改任务操作完成\n");
    }
    else
    {
        printf("操作失败\n");
    }
    return 0;
}

int delwoker(int fd , W_REGIST lg){
    MSG m1;
    m1.type = delworker;
    memcpy(m1.data, lg.name, strlen(lg.name));
    int ret = send(fd, &m1, sizeof(m1), 0);
    if (ret == -1)
    {
        perror("数据发送失败");
        return 0;
    }

   
    recv(fd, &m1, sizeof(m1), 0);
    if (m1.type == yes)
    {
        printf("已发送离职请求，请等待组长处理。\n");
    }
    else
    {
        printf("操作失败\n");
    }
}
int delwomess(int fd, W_REGIST lg)
{
    // 获取组员名
    char worker_name[128];
    strncpy(worker_name, lg.name, sizeof(worker_name) - 1);
    worker_name[sizeof(worker_name) - 1] = '\0'; // 确保字符串以 null 结尾

    // 构建组员表名
    char table_name[128];
    snprintf(table_name, sizeof(table_name), "W_%s_table", worker_name);

    // 显示任务列表
    MSG m1;
    PRINTFTASK username;
    strcpy(username.name, lg.name);
    memcpy(m1.data, &username, sizeof(username));
    m1.type = userprinttask;
    send(fd, &m1, sizeof(m1), 0);

    while (1)
    {
        if (recv(fd, &m1, sizeof(m1), 0) <= 0)
        {
            perror("接收任务信息失败");
            return -1;
        }

        if (m1.type == taskinfo)
        {
            TASK *task = (TASK *)m1.data;
            printf("任务ID: %d, 任务名称: %s, 完成进度: %s\n", task->id, task->name, task->progress);
        }
        else if (m1.type == over)
        {
            printf("任务信息已全部接收完毕\n");
            break; // 收到结束标志，退出循环
        }
        else
        {
            printf("未知的消息类型\n");
        }
    }

    // 选择要删除的任务ID
    int task_id;
    printf("请输入要删除的任务ID: ");
    scanf("%d", &task_id);
    getchar();

    // 构建删除任务的请求
    delTEST del_task;
    del_task.id = task_id;
    strcpy(del_task.name, worker_name);

    // 发送删除任务请求到服务器
    m1.type = delwotask;
    memcpy(m1.data, &del_task, sizeof(del_task));
    int ret = send(fd, &m1, sizeof(m1), 0);
    if (ret == -1)
    {
        perror("发送删除任务请求失败");
        return -1;
    }

    // 接收处理结果
    ret = recv(fd, &m1, sizeof(m1), 0);
    if (ret == -1)
    {
        perror("接收删除任务结果失败");
        return -1;
    }

    if (m1.type == yes)
    {
        printf("删除任务成功\n");
        return 0;
    }
    else
    {
        printf("删除任务失败\n");
        return -1;
    }
}